package org.example.admin.order;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.*;

public class ShowOrderInfoView extends JFrame {
    private JTable table;

    public ShowOrderInfoView() {
        setTitle("管理端订单信息查看页面");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        // 创建表格模型并设置表格数据
        DefaultTableModel model = new DefaultTableModel();
        model.addColumn("订单id");
        model.addColumn("用户id");
        model.addColumn("航班id");
        model.addColumn("乘客名");
        model.addColumn("乘客身份证");
        model.addColumn("用户电话");
        model.addColumn("预定时间");
        model.addColumn("预定状态");

        // 连接数据库并获取数据
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:Mysql://localhost:3306/airlines", "root", "123456");
            Statement statement = conn.createStatement();
            ResultSet resultSet = statement.executeQuery("SELECT * FROM air_order");

            while (resultSet.next()) {
                int orderId = resultSet.getInt("order_id");
                int userId = resultSet.getInt("user_id");
                int flightId = resultSet.getInt("flight_id");
                String passengerName = resultSet.getString("passenger_name");
                String passengerIdCardNum = resultSet.getString("passenger_id_card_num");
                String contact = resultSet.getString("contact");
                String orderTime = resultSet.getString("order_time");
                boolean status = resultSet.getBoolean("status");

                Object[] rowData = {orderId,userId,flightId,passengerName,passengerIdCardNum,contact,orderTime,status};
                model.addRow(rowData);
            }

            // 关闭连接
            resultSet.close();
            statement.close();
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 创建表格并将模型设置到表格中
        table = new JTable(model);
        JScrollPane scrollPane = new JScrollPane(table);
        add(scrollPane, BorderLayout.CENTER);

        JPanel Panel = new JPanel();
        Panel.setLayout(new FlowLayout());
        add(Panel, BorderLayout.SOUTH);

    }

    public static void main(String[] args) {
//        SwingUtilities.invokeLater(() -> {
//            ShowOrderInfoView ShowOrderInfoForm = new ShowOrderInfoView();
//            ShowOrderInfoForm.setSize(400, 300);
//            ShowOrderInfoForm.setVisible(true);
//        });
    }
}